﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using buildingLib.fee;
using DocPrinter;
using buildingLib.security;

namespace _dmis.fee
{
    public partial class ShowFeeNotify :  _dmis.UIFormBase
    {
        #region ICommand
        public static string MenuItemName()
        {
            return "通知单查询";
        }
        public static Image MenuItemImage()
        {
            return _dmis.Properties.Resources.old_edit_find.ToBitmap();
        }
        public static void MF_DoExcecute()
        {
            ShowFeeNotify dlg = new ShowFeeNotify();
            dlg.ShowDialog();
        }

        public static void MF_DoUpdateUI(ToolStripItem tsItem)
        {
            tsItem.Enabled = ACLTable.CanGet(FeeNotify.GetACLID());
        }
        #endregion
        FeeNotifyList notifyList;
        List<FeeNotifyItem> printList = new List<FeeNotifyItem>();
        int printCount = 0;
        public ShowFeeNotify()
        {
            InitializeComponent();
        }
        private void ShowFeeNotify_Load(object sender, EventArgs e)
        {
            
            SearchNotify();
            feeNotifyBindingSource.DataSource = notifyList;
        }

        private void SearchNotify()
        { 
            string where = "";
            if (txtWhereName.Text != "")
            {
                where += "name like '%" + txtWhereName.Text + "%' and ";
            }
            if (txtCreator.Text != "")
            {
                where += "creator like '%" + txtCreator.Text + "%' and ";
            }
            if (checkBoxCreatedTime.Checked)
            { 
                where += "(CreatedTime > '"+dtpCreated.Value.ToString("yyyy-MM-dd 00:00")+"' and CreatedTime < '"+dtpCreated.Value.ToString("yyyy-MM-dd 23:59")+"') and ";
            }
            where += "0=0";
            notifyList = FeeNotifyList.GetFeeNotifyList(where);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SearchNotify();
            feeNotifyBindingSource.DataSource = notifyList;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != 0)
                return;
            FeeNotifyPreview dlg;
            if (((FeeNotifyItem)itemsBindingSource.Current).Status != "已完成")
                dlg = new FeeNotifyPreview(0, true, (FeeNotifyItem)itemsBindingSource.Current);
            else
                dlg = new FeeNotifyPreview(1, false, (FeeNotifyItem)itemsBindingSource.Current);
            dlg.ShowDialog(this);
            SearchNotify();
            feeNotifyBindingSource.DataSource = notifyList;
        }

        private void llPrint_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            int count = 0;
            foreach (FeeNotifyItem item in ((FeeNotify)feeNotifyBindingSource.Current).Items)
            {
                if (item.Status == "未通知")
                {
                    count++;
                    printList.Add(item);
                }
            }
            if (count == 0)
            {
                MessageBox.Show("没有未通知的单子！","提示");
                return;
            }

            if (MessageBox.Show(this, "当前有" + count + "份未通知单子，是否打印？", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                pdlg_feeNotify.Document = pdoc_FeeNotify;
                if (pdlg_feeNotify.ShowDialog() == DialogResult.OK)
                {
                    pdoc_FeeNotify.Print();
                    SearchNotify();
                    feeNotifyBindingSource.DataSource = notifyList;
                }
            }
        }

        private void pdoc_FeeNotify_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            FeePrinter.PrintFeeNotify(e.Graphics, printList[printCount], printList[printCount].Items);
            printList[printCount].UpdateTaskStatus(1);
            printCount++;
            if (printList.Count <= printCount)
                e.HasMorePages = false;
            else
                e.HasMorePages = true;
        }

        private void feeNotifyBindingSource_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void checkBoxCreatedTime_CheckedChanged(object sender, EventArgs e)
        {
            dtpCreated.Enabled = checkBoxCreatedTime.Checked;
        }

        private void lkbDelete_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (itemsBindingSource.Current == null)
                return;
            if (((FeeNotifyItem)itemsBindingSource.Current).Status == "已作废")
                return;
            if (((FeeNotifyItem)itemsBindingSource.Current).Status == "已完成")
            {
                MessageBox.Show("该单据已经完成，无法作废！");
                return;
            }
            ((FeeNotifyItem)itemsBindingSource.Current).DeleteThis();
            SearchNotify();
            feeNotifyBindingSource.DataSource = notifyList;
        }
    }
}